<?php
class Role
{
	static function init(){
		$uid = Sessions::get('uid');
		if( $uid == 7362 ){
			return true;
		}
		
		$role_db = new DB();
		$url = $_SERVER['PHP_SELF'];
		$time = time();
		
		$role_set_time = Sessions::get('role_set_time');
		$role_url_string = Sessions::get('role_url_string');
		
		//权限的设置时间
		$created_sql = 'select created from roles_users where uid='.$uid.' order by id desc limit 1';
		$created_rs = $role_db->query($created_sql)->getOne();
		
		if( $created_rs['created'] > $role_set_time ){//权限被更新了
			//查询该用户的所有权限modelid
			$modelid_sql = 'select modelid from roles_users where uid='.$uid.' order by id desc';
			$modelid_rs = $role_db->query($modelid_sql)->getAll();
			$role_url_string = '';
			foreach( $modelid_rs as $modelid ){
				$model_sql = 'select model from roles where id='.$modelid['modelid'];
				$model_rs = $role_db->query($model_sql)->getOne();
				$role_url_string .= $model_rs['model'].',';
			}
			
			//更新时间和字符串
			Sessions::set(array('role_set_time'=>$created_rs['created']));
			Sessions::set(array('role_url_string'=>$role_url_string));
		}
		
		if( strpos($role_url_string, $url) === false ){
			return false;
		}else{
			return true;
		}
	}
}